import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';

import { loveLuckDrawStatusArrs } from '../../love-recyc-manage.pipe';
import { LuckDrawManageService } from '../luck-draw-manage.service';

@Component({
  selector: 'app-love-luckdraw-list',
  templateUrl: './love-luckdraw-list.component.html',
  styleUrls: ['./love-luckdraw-list.component.scss']
})
export class LoveLuckdrawListComponent implements OnInit {

  searchOptions = [
    { type: 1, labelText: '活动名称', valueKey: 'activityName', value: '' },
    {
      type: 2,
      labelText: '抽奖限制',
      valueKey: 'raffleLimit',
      value: [],
      multiple: false,
      notTostr: false,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [
        {value: 0, label: '需登录'},
        {value: 1, label: '无需登录'}
      ],
    },
    {
      type: 2,
      labelText: '状态',
      valueKey: 'status',
      value: [],
      multiple: true,
      notTostr: false,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [...loveLuckDrawStatusArrs]
    },
    { type: 3, labelText: '更新时间', valueKey: ['beginTime', 'endTime'], value: [], isTimeSelect: true }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  constructor(
    private msg: NzMessageService,
    private services: LuckDrawManageService
  ) { }

  ngOnInit(): void {
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getLoveLuckDrawListData(params);
    this.tableParams.loading = false;

    if (code === 0) {
      this.tableParams.tableData = data.records || [];
      this.tableParams.total = data.total || 0;
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  // 启用 0 | 禁用 4
  async changeStatus(item: any) {
    let params = {
      id: item.id,
      status: item.status === 4 ? 0 : 4
    }
    const {code, data, message}: any = await this.services.setEnableStatusData(params);

    if (code === 0) {
      this.msg.success('操作成功');
      this.loadTable();
    }
  }
}
